package com.xiaoyu.lanling.feature.chat.handler

import android.content.DialogInterface
import android.view.View
import android.view.View.OnLongClickListener
import androidx.appcompat.app.AlertDialog
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.feature.chat.data.MessageListData
import com.xiaoyu.lanling.feature.chat.model.message.ChatMessageReceiveImageItem
import com.xiaoyu.lanling.feature.chat.model.message.ChatMessageSendImageItem
import com.xiaoyu.lanling.feature.im.provider.MessageDataProvider
import com.xiaoyu.lanling.router.Router
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

object ChatMessageImageItemClickHandler {

    private val receiveImageOnLongClickListener = OnLongClickListener { v: View ->
        val itemData: ChatMessageReceiveImageItem = v.getViewTag<ChatMessageReceiveImageItem>()
                ?: return@OnLongClickListener false
        val items = arrayOf<CharSequence>(AppContext.getString(R.string.action_delete))
        AlertDialog.Builder(v.context)
                .setItems(items) { dialog: DialogInterface, which: Int ->
                    when (which) {
                        0 -> MessageDataProvider.instance.deleteMessageAsync(itemData.message)
                    }
                    dialog.dismiss()
                }.show()
        true
    }
    private val sendImageOnLongClickListener = OnLongClickListener { v: View ->
        val itemData: ChatMessageSendImageItem = v.getViewTag<ChatMessageSendImageItem>()
                ?: return@OnLongClickListener false
        val items = arrayOf<CharSequence>(AppContext.getString(R.string.action_delete))
        AlertDialog.Builder(v.context)
                .setItems(items) { dialog: DialogInterface, which: Int ->
                    when (which) {
                        0 -> MessageDataProvider.instance.deleteMessageAsync(itemData.message)
                    }
                    dialog.dismiss()
                }.show()
        true
    }

    private val receiveImageOnClickListener = View.OnClickListener { v: View ->
        val itemData: ChatMessageReceiveImageItem = v.getViewTag<ChatMessageReceiveImageItem>()
                ?: return@OnClickListener
        Single.just(MessageListData.instance.getConversationImageList())
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe { imageList ->
                    imageList?.let {
                        Router.instance.gotoPhotoActivity(v.context, itemData.imageUrl, it)
                    }
                }
    }
    private val sendImageOnClickListener = View.OnClickListener { v: View ->
        val itemData: ChatMessageSendImageItem = v.getViewTag<ChatMessageSendImageItem>()
                ?: return@OnClickListener
        Single.just(MessageListData.instance.getConversationImageList())
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe { imageList -> imageList?.let { Router.instance.gotoPhotoActivity(v.context, itemData.imageDisplayUrl, it) } }
    }

    fun bindReceiveImageItemLongClickForViewAndData(view: View, itemData: ChatMessageReceiveImageItem) {
        view.setOnLongClickListener(receiveImageOnLongClickListener)
        view.setViewTag(itemData)
    }

    fun bindSendImageItemLongClickForViewAndData(view: View, itemData: ChatMessageSendImageItem) {
        view.setOnLongClickListener(sendImageOnLongClickListener)
        view.setViewTag(itemData)
    }

    fun bindReceiveImageItemClickForViewAndData(view: View, itemData: ChatMessageReceiveImageItem) {
        view.setOnClickListener(receiveImageOnClickListener)
        view.setViewTag(itemData)
    }

    fun bindSendImageItemClickForViewAndData(view: View, itemData: ChatMessageSendImageItem) {
        view.setOnClickListener(sendImageOnClickListener)
        view.setViewTag(itemData)
    }
}